package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduWarningRuleDTO;
import com.zenith.edu.po.vo.EduWarningRuleVO;
import com.zenith.edu.service.EduWarningRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/warning-rule")
@Tag(name = "预警规则管理", description = "预警规则管理接口")
public class EduWarningRuleController {
    
    @Autowired
    private EduWarningRuleService eduWarningRuleService;
    
    @SecurePostMapping(value = "/createWarningRule", roles = "ROLE_ADMIN", permission = "warning-rule:create")
    @Operation(summary = "创建预警规则")
    public Result<EduWarningRuleVO> createWarningRule(@RequestBody EduWarningRuleDTO dto) {
        EduWarningRuleVO warningRuleVO = eduWarningRuleService.createWarningRule(dto);
        return Result.success(warningRuleVO);
    }
    
    @SecurePostMapping(value = "/getWarningRuleById/{id}", roles = "ROLE_ADMIN", permission = "warning-rule:read")
    @Operation(summary = "根据ID获取预警规则详情")
    public Result<EduWarningRuleVO> getWarningRuleById(@PathVariable Long id) {
        EduWarningRuleVO warningRuleVO = eduWarningRuleService.getWarningRuleById(id);
        if (warningRuleVO == null) {
            return Result.fail("预警规则不存在");
        }
        return Result.success(warningRuleVO);
    }
    
    @SecurePostMapping(value = "/updateWarningRule", roles = "ROLE_ADMIN", permission = "warning-rule:update")
    @Operation(summary = "更新预警规则信息")
    public Result<EduWarningRuleVO> updateWarningRule(@RequestBody EduWarningRuleDTO dto) {
        EduWarningRuleVO warningRuleVO = eduWarningRuleService.updateWarningRule(dto);
        return Result.success(warningRuleVO);
    }
    
    @SecurePostMapping(value = "/deleteWarningRule/{id}", roles = "ROLE_ADMIN", permission = "warning-rule:delete")
    @Operation(summary = "删除指定预警规则")
    public Result<Void> deleteWarningRule(@PathVariable Long id) {
        boolean result = eduWarningRuleService.deleteWarningRule(id);
        if (!result) {
            return Result.fail("预警规则删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getWarningRuleList", roles = "ROLE_ADMIN", permission = "warning-rule:read")
    @Operation(summary = "获取预警规则列表")
    public Result<List<EduWarningRuleVO>> getWarningRuleList() {
        List<EduWarningRuleVO> warningRuleList = eduWarningRuleService.getWarningRuleList();
        return Result.success(warningRuleList);
    }
}